<?php
/**
 * Created by PhpStorm.
 * User: ye
 * Date: 2017/11/19
 * Time: 下午9:49
 */

namespace App\Http\Controllers;


use App\Book;
use App\BookUser;
use App\Qiu;
use App\Third\Ifff;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class ZengController extends CheckController {

    // 增加翻页
    public function index(Request $request) {
        $perpage = $request->input('perpage', 10);
        $next_cursor = $request->input('next_cursor', 1);

        $books = $this
            ->userinfo
            ->books()
            ->skip(($next_cursor -1)* $perpage)
            ->take($perpage)
            ->get();

        return $this->output(
            200,
            '获取所有图书', [
                'books' => $books,
                'has_more' => count($books) == $perpage,
                'next_cursor' => $next_cursor + 1
            ]
        );
    }


    /**
     * 保存赠送图书，和求书的接口
     * @return  json
     * @param Request $request
     */
    public function store(Request $request) {
        $id = $request->input('id', -1);

        if ($id == -1) {
            return $this->output(400, '数据缺失');
        }

        $book = BookUser::with('book', 'qius.user')->find($id);

        if ($book) {
            $owner = $this->userinfo->id == $book->user_id ? 1 : 0;
            if ($owner == 1) {
                // 表示赠送他人过程
                $book->has_zenged = $book->has_zenged == 0 ? 1 : 0;

                $book->save();
                $message = $book->has_zenged == 0 ? '取消赠送成功' : '与人玫瑰手有余香，感谢您准备将书赠送他人';
                return $this->output(200, $message, [
                    'book' => $book,
                    'status' => $book->has_zenged == 0 ? 0 : 1,
                    'status_message' => config('xcx.status_message')[$book->has_zenged == 0 ? 0 : 1]
                ]);
            } else {
                if ($book->has_zenged == 0) {
                    return $this->output(405, '这本书还没设置对外赠送', [
                        'book' => $book,
                        'status' => 4,
                        'status_message' => config('xcx.status_message')[4]
                    ]);
                }
                // todo 表示求书过程
                $qiu = Qiu::withTrashed()
                    ->where('user_id', $this->userinfo->id)
                    ->where('book_user_id', $id)
                    ->first();
                if ($qiu) {
                    if ($qiu->trashed()) {
                        $qiu->restore();
                        return $this->output(200, '已经向书的主人发送请求了', [
                            'book' => $book,
                            'status' => 2,
                            'status_message' => config('xcx.status_message')[2]
                        ]);
                    } else {
                        $qiu->delete();
                        return $this->output(200, '您已经取消求书申请了', [
                            'book' => $book,
                            'status' => 3,
                            'status_message' => config('xcx.status_message')[3]
                        ]);
                    }
                } else {
                    $qiu = new Qiu;
                    $qiu->user_id = $this->userinfo->id;
                    $qiu->book_user_id = $id;

                    $qiu->save();
                    if ($qiu) {
                        return $this->output(200, '已经向书的主人发送求书邀请了', [
                            'book' => $book,
                            'status' => 2,
                            'status_message' => config('xcx.status_message')[2]
                        ]);
                    } else {
                        return $this->output(404, '求书失败', [
                            'book' => $book,
                            'status' => 5,
                            'status_message' => config('xcx.status_message')[5]
                        ]);
                    }
                }
            }
        }

        return $this->output(401, '数据未找到');
    }

    public function show($id) {

    }
}